﻿using ModelContextProtocol.Server;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Globalization;
using System.Text.Json;

namespace MCPSSETest.Tools;

[McpServerToolType]
public sealed class WeatherTools
{
    [McpServerTool, Description("Get weather alerts for a US state.")]
    public static async Task<string> GetAlerts(
        HttpClient client,
        [Description("The US state to get alerts for.")] string state)
    {
        using var jsonDocument = await client.ReadJsonDocumentAsync($"/alerts/active/area/{state}");
        var jsonElement = jsonDocument.RootElement;
        var alerts = jsonElement.GetProperty("features").EnumerateArray();

        if (!alerts.Any())
        {
            return "No active alerts for this state.";
        }

        return JsonConvert.SerializeObject(alerts.Select(alert =>
        {
            JsonElement properties = alert.GetProperty("properties");
            return new {
                Event = properties.GetProperty("event").GetString(),
                Area = properties.GetProperty("areaDesc").GetString(),
                Severity = properties.GetProperty("severity").GetString(),
                Description = properties.GetProperty("description").GetString(),
                Instruction = properties.GetProperty("instruction").GetString()
                };
            }
        ));
    }

    [McpServerTool, Description("Get weather forecast for a location.")]
    public static async Task<string> GetForecast(
        HttpClient client,
        [Description("Latitude of the location.")] double latitude,
        [Description("Longitude of the location.")] double longitude)
    {
        var pointUrl = string.Create(CultureInfo.InvariantCulture, $"/points/{latitude},{longitude}");
        using var jsonDocument = await client.ReadJsonDocumentAsync(pointUrl);
        var forecastUrl = jsonDocument.RootElement.GetProperty("properties").GetProperty("forecast").GetString()
            ?? throw new Exception($"No forecast URL provided by {client.BaseAddress}points/{latitude},{longitude}");
        
        using var forecastDocument = await client.ReadJsonDocumentAsync(forecastUrl);
        var periods = forecastDocument.RootElement.GetProperty("properties").GetProperty("periods").EnumerateArray();

        return JsonConvert.SerializeObject(periods.Select(period => 
            new { 
                name = period.GetProperty("name").GetString(),
                Temperature = $"{period.GetProperty("temperature").GetInt32()}°F" ,
                Wind = $"{period.GetProperty("windSpeed").GetString()} {period.GetProperty("windDirection").GetString()}",
                Forecast = $"{period.GetProperty("detailedForecast").GetString()}"
            }
        ));
    }
    [McpServerTool, Description("Get a list of users")]
    public static async Task<string> GetUserList()
    {
        var users = new[]
        {
            new { Id = 1, Name = "张三", Age = 23, Department = "计算机科学", Email = "zhangsan@example.com" ,CourseCode="PHYS301" },
            new { Id = 2, Name = "李四", Age = 25, Department = "数学", Email = "lisi@example.com",CourseCode="CS101" },
            new { Id = 3, Name = "王五", Age = 22, Department = "物理", Email = "wangwu@example.com" ,CourseCode="MATH201"},
            new { Id = 4, Name = "赵六", Age = 24, Department = "化学", Email = "zhaoliu@example.com",CourseCode="CHEM401" }
        };
        return JsonConvert.SerializeObject(users.Select(user => new {
            id =  user.Id,
            name =  user.Name,
            age =  user.Age,
            department =  user.Department,
            email =  user.Email
        }));  
    }

    [McpServerTool, Description("Get a list of courses")]
    public static async Task<string> GetCourse()
    {
        var courses = new[]
        {
            new { Code = "CS101", Name = "计算机科学导论", Instructor = "张教授", Credits = 3, Schedule = "周一、周三 9:00-10:30" },
            new { Code = "MATH201", Name = "高等数学", Instructor = "李教授", Credits = 4, Schedule = "周二、周四 13:00-14:30" },
            new { Code = "PHYS301", Name = "量子物理", Instructor = "王教授", Credits = 3, Schedule = "周一、周五 10:00-11:30" },
            new { Code = "CHEM401", Name = "有机化学", Instructor = "赵教授", Credits = 4, Schedule = "周三、周五 14:00-15:30" }
        };

        return JsonConvert.SerializeObject(courses.Select(course => new {
            code = course.Code,
            name =  course.Name,
            teacher =  course.Instructor,
            credits =  course.Credits,
            schedule =  course.Schedule
        }));
    }
}